package ru.dc.gui.ncurses;

import charva.awt.*;
import charva.awt.event.ActionEvent;
import charva.awt.event.ActionListener;
import charvax.swing.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import ru.dc.HubConnection;
import ru.dc.object.events.manager.AddConnection;
import ru.dc.object.events.manager.ConnectionManagerEvent;
import ru.dc.object.ConnectionManagerEventListener;
import ru.dc.object.ServerInfo;
import ru.dc.object.events.manager.RemoveConnection;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 11.10.2010
 * Time: 13:17:59
 * </pre>
 */
public class MainFrame extends JFrame {
    private JTabbedPane tabs;
    private Log logger = LogFactory.getLog(getClass());
    private Map<ServerInfo, JPanel> tabsByServerInfo = new HashMap<ServerInfo, JPanel>();

    public MainFrame() {

        super("YADIC");

        setForeground(Color.green);
        setBackground(Color.black);

        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 2, 1));

        tabs = new JTabbedPane();
        //tabs.setWidth(getWidth()-2);
        //tabs.setHeight(getHeight()-3);
        getContentPane().add(tabs);

        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("File");
        file.setMnemonic('F');

        JMenuItem connect = new JMenuItem("Connect", 'C');
        connect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                ConnectionParamsDlg dlg = new ConnectionParamsDlg(MainFrame.this);
                logger.debug("Created connection param dialog");
                if (dlg.execute()== JOptionPane.OK_OPTION) {
                    ServerInfo info = new ServerInfo();
                    info.setAddress(dlg.getAddress());
                    info.setEncoding("windows-1251");
                    info.setNick(dlg.getNick());
                    final HubConnection connection = new HubConnection();
                    connection.setServerInfo(info);
/*
                    Application.getApplication().getConnectionManager().addConnection(connection);
                    new Thread() {
                        @Override
                        public void run() {
                            try {
                                connection.connect();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();
*/
                }
            }
        });
        file.add(connect);
        file.addSeparator();

        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                System.exit(0);
            }
        });
        file.add(exit);
        menuBar.add(file);
        setJMenuBar(menuBar);

        
        Application.getApplication().getConnectionManager().addEventListener(new ConnectionManagerEventListener() {
            public void onEvent(ConnectionManagerEvent event) {
                
                if (event instanceof AddConnection) {
                    AddConnection addConnection = (AddConnection) event;
                    ServerInfo serverInfo = addConnection.getServerInfo();
                    if (tabsByServerInfo.get(serverInfo)==null) {
                        final String key = addConnection.getServerInfo().getAddress();
                        final HubWindow window = new HubWindow();
/*
                        window.setServerInfo((HubConnection) addConnection.getConnection());
                        tabs.addTab(key, null, window, key);
                        tabsByServerInfo.put(addConnection.getConnection().getServerInfo(), window);
*/
                    }
                }


                else if (event instanceof RemoveConnection) {
                    RemoveConnection removeConnection = (RemoveConnection) event;
/*
                    ServerInfo serverInfo = removeConnection.getConnection().getServerInfo();
                    if (tabsByServerInfo.get(serverInfo)!=null) {
                        HubWindow w = (HubWindow) tabsByServerInfo.get(serverInfo);
                        w.close();
                        tabs.remove(w);
                    }
*/


                }
            }
        });
        

        setWidth(Toolkit.getDefaultToolkit().getScreenColumns());
        setHeight(Toolkit.getDefaultToolkit().getScreenRows());
        //setSize(80, 48);
        setLocation(0, 0);
        validate();
    }




}
